// 02多级对象扁平化

const menu ={
    FirstMenu:{
        name:'fruit',
        subMenu1:{
            name:'apple'
        }
    },

    SecondMenu:{
        name:'vagetables',
        subMenu1:{
            name:'carrot'
        }
    }
}

function Method(obj,update =''){
    
    if(Object.prototype.toString.call(obj)!=='[object Object]'){
        return console.log('请传入一个对象！')
    }

    const result ={}

    for(const key in obj){

        if(obj.hasOwnProperty(key)){

            const newKey = update ? `${update}.${key}` : key;

            if(Object.prototype.toString.call(obj[key])==='[object Object]' && obj[key]!== null && !Array.isArray(obj[key])){
                
                Object.assign(result,Method(obj[key],newKey))
            
            }else{
                result[newKey]=obj[key]
            }


        }
    }

    return  result
}

console.log(Method(menu));